from fastapi.responses import JSONResponse
from pydantic import conint

from common.base.base_routers import get_custom_router
from services.ad936x.api import Ad936xApi
from services.ad936x.schemas.configs import DoubleConfig, SingleConfig, DoubleFrequency, SingleFrequency, DoubleGain, \
    SingleGain, DoubleAttenuation, SingleAttenuation
from settings import settings


ad_api = Ad936xApi()
ad936x_router = get_custom_router(ad_api, prefix='/ad936x', tags=['ad936x'])


@ad936x_router.post('/configure/', response_class=JSONResponse)
async def configure(index: conint(ge=0, lt=settings.AD936X_NUMBER),
                    config: DoubleConfig | SingleConfig):
    return await ad_api.configure(index, config)


@ad936x_router.post('/set-center-frequency/', response_class=JSONResponse)
async def set_center_frequency(index: conint(ge=0, lt=settings.AD936X_NUMBER),
                               config: DoubleFrequency | SingleFrequency):
    return await ad_api.set_center_frequency(index, config)


@ad936x_router.post('/set-gain/', response_class=JSONResponse)
async def set_gain(index: conint(ge=0, lt=settings.AD936X_NUMBER),
                   config: DoubleGain | SingleGain):
    return await ad_api.set_gain(index, config)


@ad936x_router.post('/set-attenuation/', response_class=JSONResponse)
async def set_attenuation(index: conint(ge=0, lt=settings.AD936X_NUMBER),
                          config: DoubleAttenuation | SingleAttenuation):
    return await ad_api.set_attenuation(index, config)


@ad936x_router.get('/get-all-configs/', response_class=JSONResponse)
async def get_all_configs():
    return await ad_api.get_all_configs()


@ad936x_router.get('/get-config/', response_class=JSONResponse)
async def get_config(index: conint(ge=0, lt=settings.AD936X_NUMBER)):
    return await ad_api.get_config(index)

@ad936x_router.get('/digital-tune-all/', response_class=JSONResponse)
async def digital_tune_all():
    return await ad_api.digital_tune_all()

@ad936x_router.get('/set-night-digital-tune/', response_class=JSONResponse)
async def set_night_digital_tune(night_digital_tune: bool):
    return await ad_api.set_night_digital_tune(night_digital_tune)

@ad936x_router.get('/get-night-digital-tune/', response_class=JSONResponse)
async def get_night_digital_tune():
    return await ad_api.get_night_digital_tune()

@ad936x_router.post('/set-digital-tune-range/', response_class=JSONResponse)
async def set_digital_tune_range(index: int, digital_tune_range: tuple[int, int, int, int]):
    return await ad_api.set_digital_tune_range(index, digital_tune_range)

@ad936x_router.get('/get-digital-tune-range/', response_class=JSONResponse)
async def get_digital_tune_range(index: int):
    return await ad_api.get_digital_tune_range(index)
